import axios from "axios";
import { Spin } from "antd";
const instance = axios.create({
  baseURL: "http://blogapi.shbwyz.com",
  timeout: 10000,
  headers: { "X-Custom-Header": "foobar" },
});



// 添加请求拦截器
instance.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    const token = "Bearer " + localStorage.getItem("token");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);
// 添加响应拦截器
instance.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么

    return response;
  },
  function (error) {
    // 对响应错误做点什么
    if (error.response) {
      const status = error.response.data;
      switch (status) {
        case 400:
          console.log("请求错误");
          break;
        case 500:
          console.log("状态码错误");
          break;
      }
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log("Error", error.message);
    }
    return Promise.reject(error);
  }
);

export default instance;
